// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers'])

    .run(['$ionicPlatform', '$ionicPopup','$rootScope','$location','$ionicLoading', function ($ionicPlatform, $ionicPopup, $rootScope, $location,$ionicLoading) {
        $ionicPlatform.ready(function() {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            if(window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if(window.StatusBar) {
                // org.apache.cordova.statusbar required
                StatusBar.styleDefault();
            }


        });

        //主页面显示退出提示框
        $ionicPlatform.registerBackButtonAction(function (e) {
            e.preventDefault();

            function showConfirm() {
                var confirmPopup = $ionicPopup.confirm({
                    title: '<strong>退出系统?</strong>',
                    template: '您确定要退出系统吗?',
                    okText: '&nbsp;&nbsp;退出&nbsp;&nbsp;',
                    cancelText: '&nbsp;&nbsp;取消&nbsp;&nbsp;'
                });
                confirmPopup.then(function (res) {
                    if (res) {
                        ionic.Platform.exitApp();
                    } else {
                        // Don't close
                    }
                });
            }
            if ($rootScope.$viewHistory.backView ) {
                console.log('currentView:', $rootScope.$viewHistory.currentView);
                // Go back in history
                $rootScope.$viewHistory.backView.go();
            } else {
                // This is the last page: Show confirmation popup
                showConfirm();
            }


            return false;
        }, 101);

    }])


    .config(function ($stateProvider, $urlRouterProvider,$httpProvider) {
        //设置跨域
//        $httpProvider.defaults.useXDomain = true;
//        delete $httpProvider.defaults.headers.common['X-Requested-With'];
//        //设置表单数据提交
//        $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
//        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        $stateProvider
            .state('welcome', {
                url: '/',
                templateUrl: 'templates/welcome.html',
                controller: 'welcomeCtrl'
            })
            .state('app', {
                url: "/app",
                abstract: true,
                templateUrl: "templates/menu.html",
                controller: 'AppCtrl'
            })
            .state('login', {
                url: "/login",
                templateUrl: "templates/login.html",
                controller: 'loginCtrl'
            })

            .state('app.boxs', {
                url: "/boxs",
                views: {
                    'menuContent': {
                        templateUrl: "templates/boxs.html",
                        controller: 'boxsCtrl'
                    }
                }
            })
            .state('app.box', {
                url: "/box/:box_id",
                views: {
                    'menuContent': {
                        templateUrl: "templates/box.html",
                        controller: 'boxCtrl'
                    }
                }
            })
            .state('app.cost', {
                url: "/cost/:box_id",
                views: {
                    'menuContent': {
                        templateUrl: "templates/cost.html",
                        controller: 'costCtrl'
                    }
                }
            })
            .state('app.doorState', {
                url: "/doorState/:box_id",
                views: {
                    'menuContent': {
                        templateUrl: "templates/doorState.html",
                        controller: 'doorStateCtrl'
                    }
                }
            })
            .state('app.addr', {
                url: "/addr/:box_id/:addr",
                views: {
                    'menuContent': {
                        templateUrl: "templates/addr.html",
                        controller: 'addrCtrl'
                    }
                }
            })
            .state('app.fault', {
                url: "/fault",
                views: {
                    'menuContent': {
                        templateUrl: "templates/fault.html",
                        controller: 'faultCtrl'
                    }
                }
            })
            .state('app.store', {
                url: "/store",
                views: {
                    'menuContent': {
                        templateUrl: "templates/store.html",
                        controller: 'storeCtrl'
                    }
                }
            })
            .state('app.addGoods', {
                url: "/addGoods/:gid",
                views: {
                    'menuContent': {
                        templateUrl: "templates/addGoods.html",
                        controller: 'addGoodsCtrl'
                    }
                }
            })
            .state('app.goods', {
                url: "/goods/:gid",
                views: {
                    'menuContent': {
                        templateUrl: "templates/goods.html",
                        controller: 'goodsCtrl'
                    }
                }
            })
            .state('app.goodsPar', {
                url: "/goodsPar/:id",
                views: {
                    'menuContent': {
                        templateUrl: "templates/goodsPar.html",
                        controller: 'goodsParCtrl'
                    }
                }
            })
            .state('app.changGoods', {
                url: "/changGoods/:id",
                views: {
                    'menuContent': {
                        templateUrl: "templates/changGoods.html",
                        controller: 'changGoodsCtrl'
                    }
                }
            })
            .state('app.center', {
                url: "/center",
                views: {
                    'menuContent': {
                        templateUrl: "templates/center.html",
                        controller: 'centerCtrl'
                    }
                }
            })
            .state('app.sellGoods', {
                url: "/sellGoods",
                views: {
                    'menuContent': {
                        templateUrl: "templates/sellGoods.html",
                        controller: 'sellGoodsCtrl'
                    }
                }
            })
            .state('app.sellStore', {
                url: "/sellStore",
                views: {
                    'menuContent': {
                        templateUrl: "templates/sellStore.html",
                        controller: 'sellStoreCtrl'
                    }
                }
            })
            .state('app.selectGoods', {
                url: "/selectGoods/:gid",
                views: {
                    'menuContent': {
                        templateUrl: "templates/selectGoods.html",
                        controller: 'selectGoodsCtrl'
                    }
                }
            })
            .state('app.sellGoodsPar', {
                url: "/sellGoodsPar/:id",
                views: {
                    'menuContent': {
                        templateUrl: "templates/sellGoodsPar.html",
                        controller: 'sellGoodsParCtrl'
                    }
                }
            })


            .state('app.playlists', {
                url: "/playlists",
                views: {
                    'menuContent': {
                        templateUrl: "templates/playlists.html",
                        controller: 'PlaylistsCtrl'
                    }
                }
            })
            .state('app.single', {
                url: "/playlists/:playlistId",
                views: {
                    'menuContent': {
                        templateUrl: "templates/playlist.html",
                        controller: 'PlaylistCtrl'
                    }
                }
            });
        // if none of the above states are matched, use this as the fallback
        $urlRouterProvider.otherwise('/login');
    });

